package com.owen.system.keep.history.controller;

import com.owen.common.utils.PageUtils;
import com.owen.common.utils.Query;
import com.owen.common.utils.R;
import com.owen.system.keep.account.service.KeepAccountService;
import com.owen.system.keep.history.dto.KeepConsumptionHistoryDTO;
import com.owen.system.keep.history.service.KeepConsumptionHistoryService;
import com.owen.system.keep.history.vo.KeepConsumptionHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "收支记录")
@RequestMapping("/keep/consumptionHistory")
public class KeepConsumptionHistoryController {

    @Autowired
    private KeepConsumptionHistoryService keepConsumptionHistoryService;

    // 账户
    @Autowired
    private KeepAccountService keepAccountService;

    @GetMapping
    @RequiresPermissions("keep:consumptionHistory:list")
    @ApiOperation(value = "进入我的收支记录页面", notes = "进入我的收支记录页面", httpMethod = "GET")
    String listPage() {
        return "keep/history/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("keep:consumptionHistory:list")
    @ApiOperation(value = "收支记录列表", notes = "收支记录列表", httpMethod = "GET")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<KeepConsumptionHistoryVO> KeepConsumptionHistoryList = keepConsumptionHistoryService.list(query);
        int total = keepConsumptionHistoryService.count(query);
        PageUtils pageUtils = new PageUtils(KeepConsumptionHistoryList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("keep:consumptionHistory:add")
    @ApiOperation(value = "进入新增收支记录页面", notes = "进入新增收支记录页面", httpMethod = "GET")
    String add() {
        return "keep/history/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("keep:consumptionHistory:edit")
    @ApiOperation(value = "进入编辑收支记录页面", notes = "进入编辑收支记录页面", httpMethod = "GET")
    String edit(@PathVariable("id") String id, Model model) {
        KeepConsumptionHistoryVO keepConsumptionHistory = keepConsumptionHistoryService.get(id);
        model.addAttribute("keepConsumptionHistory", keepConsumptionHistory);
        return "keep/history/edit";
    }

    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("keep:consumptionHistory:add")
    @ApiOperation(value = "新增收支记录", notes = "新增收支记录", httpMethod = "POST")
    public R save(KeepConsumptionHistoryDTO dto) {
        if (keepConsumptionHistoryService.save(dto) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/update")
    @ApiOperation(value = "修改收支记录", notes = "修改收支记录", httpMethod = "POST")
    @RequiresPermissions("keep:consumptionHistory:edit")
    public R update(KeepConsumptionHistoryDTO dto) {
        keepConsumptionHistoryService.update(dto);
        return R.ok();
    }

    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("keep:consumptionHistory:remove")
    @ApiOperation(value = "删除收支记录", notes = "删除收支记录", httpMethod = "POST")
    public R remove(String id) {
        if (keepConsumptionHistoryService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("keep:consumptionHistory:batchRemove")
    @ApiOperation(value = "批量删除收支记录", notes = "批量删除收支记录", httpMethod = "POST")
    public R remove(@RequestParam("ids[]") String[] ids) {
        keepConsumptionHistoryService.batchRemove(ids);
        return R.ok();
    }

}
